package com.duan.laugh.event.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duan.laugh.common.core.util.Assert;
import com.duan.laugh.common.core.util.Query;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.event.pojo.entity.AfterEvent;
import com.duan.laugh.event.service.IAfterEventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后续事件 controller
 * </p>
 *
 * @author liutf
 * @since 2019-11-12
 */
@Api(tags = "后续事件")
@RestController
@RequestMapping("/after-event")
public class AfterEventController {
    @Autowired
    private IAfterEventService afterEventService;

    /**
     * 新增后续事件
     *
     * @param afterEvent 实体
     * @return true/false
     */
    @PostMapping
    @ApiOperation("新增后续事件")
    public R save(@RequestBody AfterEvent afterEvent) {
        return R.ok(afterEventService.save(afterEvent));
    }

    /**
     * 根据id查看后续事件
     *
     * @param id
     * @return AfterEvent
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查看后续事件")
    public R<AfterEvent> getById(@PathVariable String id) {
        return R.ok(afterEventService.getById(id));
    }

    /**
     * 分页查询后续事件
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    @ApiOperation("分页查询后续事件")
    public IPage<AfterEvent> page(@RequestParam Map<String, Object> params) {
        return afterEventService.page(new Query<>(params));
    }

    /**
     * 根据id删除后续事件
     *
     * @param id
     * @return true/false
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除后续事件")
    public R removeById(@PathVariable String id) {
        return R.ok(afterEventService.removeById(id));
    }

    /**
     * 根据id修改后续事件
     *
     * @param afterEvent 实体
     * @return true/false
     */
    @PutMapping
    @ApiOperation("根据id修改后续事件")
    public R updateById(@RequestBody AfterEvent afterEvent) {
        return R.ok(afterEventService.updateById(afterEvent));
    }

    /**
     * 0010306 根据事件处理规则id查询后续事件
     *
     * @param id
     * @return AfterEvent
     */
    @GetMapping("/event-handler-rule/{id}")
    @ApiOperation("0010306 根据事件处理规则id查询后续事件")
    public R<List<AfterEvent>> selectListByEventHandlerRuleId(@PathVariable String id) {
        return R.ok(afterEventService.selectListByEventHandlerRuleId(id));
    }

    /**
     * 0010307 根据事件处理规则ids批量删除后续事件
     *
     * @param params
     * @return AfterEvent
     */
    @DeleteMapping("/event-handler-rule/")
    @ApiOperation("0010307 根据事件处理规则ids批量删除后续事件")
    public R<Boolean> deleteByEventHandlerRuleIds(@RequestBody Map<String, Object> params) {
        String ids = getParamString(params, "ids");
        return R.ok(afterEventService.deleteByEventHandlerRuleIds(ids));
    }

    /**
     * 获取map里的value
     *
     * @param params map
     * @param param  目标
     * @return string
     */
    private String getParamString(Map params, String param) {
        String value = String.valueOf(params.get(param));
        Assert.isBlank(value, param + "不能为空");
        return value;
    }
}

